$(function(){

    var page = 1;
    var pageSize = 10;
    var countNum = null;

    getData();

    $("#nextBtn").on("click",function(){
        page++;
        if(page > countNum){
            page = countNum;
            alert("已经是最后一页了");
            return;
        }
        getData();
    });

    $("#prevBtn").on("click",function(){
        page--;
        if(page < 1){
            page = 1;
            alert("已经是第一页了");
            return;
        }
        getData();
    });

    function getData(){
        $.ajax({
            type: "get",
            url: "/category/querySecondCategoryPaging",
            data: {
                page : page,
                pageSize :pageSize
            },
            success: function (res) {
                console.log(res);
                countNum = Math.ceil(res.total / pageSize);
                var html = template("categorySecondTpl",res);
                $("#categorySecondBox").html(html);
                
            }
        });
    }

    $.ajax({
        type: "get",
        url: "/category/queryTopCategoryPaging",
        data: {
            page : 1,
            pageSize: 100,
        },
        success: function (res) {
           
            var html = template("categoryFirstTpl",res);
            $("#categoryFirstBox").html(html);
        }
    });

    var previewImg = "";
    
    $("#fileUpload").fileupload({
        dataType: 'json',
        done : function(e,data){
            //console.log(data.result.picAddr);
            $("#preview").attr("src",data.result.picAddr);
            previewImg = data.result.picAddr;
        }
    });

    $("#fileUpload").change(function (e) { 
        e.preventDefault();
        
    });

    $("#save").on("click",function(){
        var categoryFirstName = $.trim($("[name='categoryFirstName']").val());
        var productName = $.trim($("[name='productName']").val());
        if(!categoryFirstName){
            alert("请选择名称");
            return;
        }
        if(!productName){
            alert("请选择商品名称");
            return;
        }

        $.ajax({
            type: "post",
            url: "/category/addSecondCategory",
            data: {
                brandName: productName,
                categoryId: categoryFirstName,
                brandLogo: previewImg,
                hot: 0,
            },
            success: function (res) {
                if(res.success){
                    location.reload();
                }
            }
        });

    });

    

});